package edu.uwosh.cs342.placestobe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewMeeting extends Activity implements OnClickListener{

    int meetID;
    String meetDescription;
    String meetDate;
    String meetTime;
    int meetLatitude;
    int meetLongitude;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.viewmeeting);
            Intent currentIntent = getIntent();
            
            meetID = currentIntent.getIntExtra("eventID", 0);
            meetDescription = currentIntent.getStringExtra("eventDescription");
            meetDate = currentIntent.getStringExtra("eventDate");
            meetTime = currentIntent.getStringExtra("eventTime");
            meetLatitude = currentIntent.getIntExtra("eventLatitude", 0);
            meetLongitude = currentIntent.getIntExtra("eventLongitude", 0);
            
            meetDate = formatDate(meetDate);
            meetTime = formatTime(meetTime);
            
            TextView meetTextView = (TextView) findViewById(R.id.meetIdTextView);
            meetTextView.setText("Meeting ID:  " + meetID + "\n");
            
            TextView descriptionTextView = (TextView) findViewById(R.id.meetDescriptionTextView);
            descriptionTextView.setText("Meeting Description:  \n\t\t\t\t\t" + meetDescription + "\n");
            
            TextView dateTextView = (TextView) findViewById(R.id.meetDateTextView);
            dateTextView.setText("Meeting Date:  \n\t\t\t\t\t" + meetDate + "\n");
            
            TextView timeTextView = (TextView) findViewById(R.id.meetTimeTextView);
            timeTextView.setText("Meeting Time:  \n\t\t\t\t\t" + meetTime + "\n");
            
            Button cancelButton = (Button) findViewById(R.id.cancelMeetingButton);
            cancelButton.setOnClickListener(this);
            
            Button backButton = (Button) findViewById(R.id.backMeetingButton);
            backButton.setOnClickListener(this);
    }

    private String formatTime(String meetTime2) {
            String formattedTime = "";
            String ampm = "AM";
            
            int token = meetTime2.indexOf(":");
            
            int hour = Integer.parseInt(meetTime2.substring(0, token));
            
            if(hour > 13){
                    hour = hour - 12;
                    ampm = "PM";
            }
            
            formattedTime = hour + meetTime2.substring(token) + " " + ampm;

            return formattedTime;
    }

    private String formatDate(String meetDate2) {
            String formattedDate = "";
            int token = meetDate2.indexOf("-");
            
            String rest = meetDate2.substring(token+1);
            
            formattedDate = getMonth(Integer.parseInt(meetDate2.substring(0, token)));
            
            int token2= rest.indexOf("-");
            formattedDate = formattedDate + rest.substring(0, token2) + ", ";
            formattedDate = formattedDate + rest.substring(token2 + 1);
            
            return formattedDate;
    }
    
    String getMonth(int monthNum){
            String selectedMonth;
            
            switch(monthNum){
            case 1:
                    selectedMonth = "January ";
                    break;
            case 2:
                    selectedMonth = "February ";
                    break;
            case 3:
                    selectedMonth = "March ";
                    break;
            case 4:
                    selectedMonth = "April ";
                    break;
            case 5:
                    selectedMonth = "May ";
                    break;
            case 6:
                    selectedMonth = "June ";
                    break;
            case 7:
                    selectedMonth = "July ";
                    break;
            case 8:
                    selectedMonth = "August ";
                    break;
            case 9:
                    selectedMonth = "September ";
                    break;
            case 10:
                    selectedMonth = "October ";
                    break;
            case 11:
                    selectedMonth = "November ";
                    break;
            default:
                    selectedMonth = "December ";
                    break;
            }
            return selectedMonth;
    }

    public void onClick(View v) {
            // TODO Auto-generated method stub
    	
    	String res;
            switch(v.getId()){
            case R.id.cancelMeetingButton:
            	try {
					res = CustomHttpClient
					.executeHttpGet("http://cs346.cs.uwosh.edu/students/blairm83/cs342group6/deleteEvent.php?meetID="+ meetID);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	Toast.makeText(getApplicationContext(),
						"Meeting Deleted",
						Toast.LENGTH_LONG).show();
            	finish();
                    break;
            case R.id.backMeetingButton:
                    finish();
                    break;
            }
    }

}
